package com.yc.pre.config.security;

import com.yc.pre.entity.bean.PreSignUser;
import com.yc.pre.mapper.primary.PreSignUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.Optional;

/**
 * @ClassName UsernamePasswordDetailsService
 * @Description 继承UserDetailsService，重写认证方法
 * @Date 2021/02/08 19:16:38
 * @Created by ccc-j
 * @email ccc-ju@outlook.com
 */
public class UsernamePasswordDetailsService implements UserDetailsService {

    @Autowired
    private PreSignUserMapper preSignUserMapper;

    @Override
    public UserDetails loadUserByUsername(String phone) throws UsernameNotFoundException {
        PreSignUser user = Optional.ofNullable(preSignUserMapper.findPreSignUserByPhone(phone)).orElseThrow(() -> new UsernameNotFoundException("账号不存在：" + phone));

        return new PreSignUserDetails(user);
    }
}
